﻿using UnityEngine;
using System.Collections;

public class GameManager : Singleton<GameManager> {
    public GameObject background;
    public GameObject Panel;
    public GameObject tempbomb;
    public GameObject labelTime;

    [HideInInspector]
    private int[,] MatrixGame;

    [HideInInspector]
    public static LevelManager levelManager;

    [HideInInspector]
    public int rows = 10;
    [HideInInspector]
    public int cols = 10;
	// Use this for initialization
    [HideInInspector]
    private int playSecond;
    [HideInInspector]
    private int playMinute;
    [HideInInspector]
    private float DelayTime = 3;
    private float currTime = 0;
	void Start () {

        initVariables();
        InitMap();
        StartCoroutine(CalculateTimePlay());
	}
    void initVariables()
    {
        playMinute = 0;
        playSecond = 0;
        MatrixGame = new int[rows, cols];
    }
    IEnumerator CalculateTimePlay()
    {
        while (true)
        {
            playSecond++;
            if (playSecond > 60)
            {
                playSecond = 0;
                playMinute++;
            }
            string minuteText = playMinute < 10 ? "0" + playMinute.ToString() : playMinute.ToString();
            string secondText = playSecond < 10 ? "0" + playSecond.ToString() : playSecond.ToString();
            labelTime.GetComponent<UILabel>().text = minuteText + ":" + secondText;
            yield return new WaitForSeconds(0.1f);
        }
    }

    void InitMap()
    {
        
        float screenWidth = background.GetComponent<UIWidget>().width;
        float screenHeight = background.GetComponent<UIWidget>().height;
        
        float size = screenWidth/cols;
        float variance = size/2;
        
        float marginTop = screenHeight/2 ;
        TextAsset ts = levelManager.LevelDatabase.Waves[levelManager.iCurrentLevel - 1];
        Debug.Log("Text OK");

        string[] map = ts.text.Split('\n');

        Texture[] tt = Resources.LoadAll<Texture>(DatabaseDirectory.BombBlack);
        float scale = size / tt[0].width;
        float boxColliderSize = size / scale;
        GameObject go = null;
        for (int i = 0, k = 0; i < rows; i++)
        {
            
            for (int j = 0; j < cols; j++,k++)
            {

                MatrixGame[i, j] = int.Parse(map[k]);
                switch (MatrixGame[i, j])
                {
                    case 0:
                        continue;
                    case 1:
                        go = Instantiate(Resources.Load(DatabaseDirectory.Wall)) as GameObject;
                        go.GetComponentInChildren<UITexture>().SetDimensions((int)size, (int)size);
                        go.transform.parent = tempbomb.transform;
                        go.transform.localScale = Vector3.one;
                       
                        break;
                    case 2:
                        go = Instantiate(Resources.Load(DatabaseDirectory.PrefabBomb)) as GameObject;
                        go.GetComponent<BombController>().bombType = EBombType.BLACK;
                        go.transform.parent = tempbomb.transform;
                        go.transform.localScale = new Vector3(scale, scale, 0);
                        go.GetComponent<BoxCollider>().size = new Vector3(boxColliderSize, boxColliderSize, 0);
                        break;
                    case 3:
                        go = Instantiate(Resources.Load(DatabaseDirectory.PrefabBomb)) as GameObject;
                        go.GetComponent<BombController>().bombType = EBombType.BLUE;
                        go.transform.parent = tempbomb.transform;
                        go.transform.localScale = new Vector3(scale, scale, 0);
                        go.GetComponent<BoxCollider>().size = new Vector3(boxColliderSize, boxColliderSize, 0);
                        break;
                    case 4:
                        go = Instantiate(Resources.Load(DatabaseDirectory.PrefabBomb)) as GameObject;
                        go.GetComponent<BombController>().bombType = EBombType.GREEN;
                        go.transform.parent = tempbomb.transform;
                        go.transform.localScale = new Vector3(scale, scale, 0);
                        go.GetComponent<BoxCollider>().size = new Vector3(boxColliderSize, boxColliderSize, 0);
                        break;
                    case 5:
                        go = Instantiate(Resources.Load(DatabaseDirectory.PrefabBomb)) as GameObject;
                        go.GetComponent<BombController>().bombType = EBombType.YELLOW;
                        go.transform.parent = tempbomb.transform;
                        go.transform.localScale = new Vector3(scale, scale, 0);
                        go.GetComponent<BoxCollider>().size = new Vector3(boxColliderSize, boxColliderSize, 0);
                        break;
                    case 6:
                        go = Instantiate(Resources.Load(DatabaseDirectory.PrefabBomb)) as GameObject;
                        go.GetComponent<BombController>().bombType = EBombType.RED;
                        go.transform.parent = tempbomb.transform;
                        go.transform.localScale = new Vector3(scale, scale, 0);
                        go.GetComponent<BoxCollider>().size = new Vector3(boxColliderSize, boxColliderSize, 0);
                        break;
                    case 7:
                        go = Instantiate(Resources.Load(DatabaseDirectory.PrefabBomb)) as GameObject;
                        go.GetComponent<BombController>().bombType = EBombType.PINK;
                        go.transform.parent = tempbomb.transform;
                        go.transform.localScale = new Vector3(scale, scale, 0);
                        go.GetComponent<BoxCollider>().size = new Vector3(boxColliderSize, boxColliderSize, 0);
                        break;

                }

                go.transform.localPosition = new Vector3(size * j + variance - screenWidth / 2, -size * i - variance , 0);
            }
        }
    }

    public void Reset()
    {

    }
}
